#include <Windows.h>
#include <iostream>
#include <Windows.h>
#include <iostream>

using namespace std;

HHOOK hHook = NULL;

LRESULT CALLBACK MessageHook(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HC_ACTION && wParam != PM_NOREMOVE)
    {
        MSG *pMsg = (MSG *)lParam;
        cout << "Received message: " << pMsg->message << endl;

        cout << pMsg->message << endl;
        if (pMsg->message == 12345)
        {
            cout << "Unhooked" << endl;
            UnhookWindowsHookEx(hHook);
        }
    }

    return CallNextHookEx(hHook, nCode, wParam, lParam);
}

int main()
{
    SHELLEXECUTEINFO info = {0};
    info.cbSize = sizeof(info);
    info.fMask = SEE_MASK_NOCLOSEPROCESS;
    info.hwnd = NULL;
    info.lpVerb = NULL;
    info.lpFile = "E:/Project/Repositories/cpp/cpp/httpserver/dist/py/dist/pppppp_MinGW@13.2.0_min.exe"; // 替换为要打开的程序的路径
    info.lpParameters = NULL;
    info.lpDirectory = NULL;
    info.nShow = SW_SHOW;

    if (ShellExecuteEx(&info))
    {
        WaitForSingleObject(info.hProcess, INFINITE);
        CloseHandle(info.hProcess);
    }
    else
    {
        cout << "Failed to open program." << endl;
    }

    HINSTANCE hInstance = GetModuleHandle(NULL);
    hHook = SetWindowsHookEx(WH_GETMESSAGE, MessageHook, hInstance, 0);

    if (hHook == NULL)
    {
        std::cout << "Failed to install hook." << std::endl;
        return 1;
    }

    system("pause");

    return 0;
}